package com.miguo.component;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.miguo.activity.MainActivity;
import com.miguo.app.AppHelper;
import com.miguo.component.search.SearchTxtCycleShowView;
import com.miguo.ui.R;
import com.mrper.util.ApkUtil;

public class HomeHeaderNavView extends LinearLayout implements View.OnClickListener {

    private Paint mPaint;
    private Context mContext;
    private Activity mActivity;
    private ImageView mLeft, mRight;
    private LinearLayout mSearch;
    private SearchTxtCycleShowView mSearchTxt;
    private MainActivity mMainActivity;

    public HomeHeaderNavView(Context context) {
        this(context, null);
    }

    public HomeHeaderNavView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mActivity = (Activity) context;
        setOrientation(HORIZONTAL);
        inflate(mContext, R.layout.layer_home_head_nav, this);
        mLeft = (ImageView) findViewById(R.id.left);
        mRight = (ImageView) findViewById(R.id.right);
        mSearch = (LinearLayout) findViewById(R.id.search);
        mSearchTxt = (SearchTxtCycleShowView) findViewById(R.id.search_txt);
        mLeft.setOnClickListener(this);
        mRight.setOnClickListener(this);
        mSearch.setOnClickListener(this);
        this.mPaint = new Paint();
        this.mPaint.setAntiAlias(true);
        this.mPaint.setStyle(Paint.Style.FILL);
        //String hex = Integer.toHexString(-1447447);
        //this.mPaint.setColor(Color.parseColor("#ffe9e9e9"));
        //this.mPaint.setColor(Color.LTGRAY);
        this.mPaint.setColor(ApkUtil.getColorById(context,R.color.bg_divider_dddddd));
        this.mPaint.setStrokeWidth(1.0F);
        setWillNotDraw(false);
    }

    public void setActivity(MainActivity MainActivity) {
        mMainActivity = MainActivity;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left:
                mMainActivity.setTabsDisplay(1);
                break;
            case R.id.right:
                AppHelper.StartSign(mActivity);
                break;
            case R.id.search:
                AppHelper.StartSearch(mActivity, mSearchTxt.getKeyword());
                break;
        }
    }


    public void startAutoScroll() {
        mSearchTxt.startAutoScroll();
    }

    public void stopAutoScroll() {
        mSearchTxt.stopAutoScroll();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(0.0F, getHeight() - 1, getWidth(), getHeight() - 1, this.mPaint);
        setWillNotDraw(true);
    }
}
